package com.example.planetexploder.module.bookshelf;

import android.content.Context;
import android.util.Log;

import com.example.planetexploder.basepresenter.PSort;
import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.constants.LogConst;
import com.example.planetexploder.constants.SortConst;
import com.example.planetexploder.model.dao.Dao;
import com.example.planetexploder.model.folder.Folder;
import com.example.planetexploder.model.preference.SharedPreference;
import com.example.planetexploder.utils.IdCreator;
import com.example.planetexploder.utils.MyRandom;
import com.example.planetexploder.utils.TimeUtil;

import java.util.ArrayList;

/**
 * 书架页面Presenter
 *
 * @author 宋熙然
 * 修改日期：2019.12.22
 */
public class Presenter_BookShelf {

    private static final String TAG = "Presenter_BookShelf";

    public PSort pSort;

    private Dao dao;
    private Folder folder;
    private SharedPreference sp;

    public Presenter_BookShelf(Context context) {
        pSort = new PSort(context);
        dao = new Dao(context);
        folder = new Folder(context);
        sp = new SharedPreference(context);
    }

    /**
     * 获得摘抄集总表
     * @param tag1  不为0时按tag1的值检索
     * @param tag2  不为0时按tag2的值检索
     * @return 摘抄集总表
     * @see SortConst 排序相关常量
     */
    public ArrayList<BookInfo> get_ShelfTable(int tag1, int tag2) {
        Log.d(TAG, "get_ShelfTable() called tag1:" + tag1 + " tag2:" + tag2);

        int order = sp.appSettings.get_SortWay();
        
        Log.d(LogConst.PTAG, "get_ShelfTable()");
        return dao.shelfTable.get_ShelfTable(order, tag1, tag2);
    }

    /**
     * 新建摘抄集
     * @param bookName 用户输入的摘抄集名称
     * @return 新的摘抄集信息
     */
    public BookInfo create_Book(String bookName) {
        Log.d(LogConst.PTAG, "create_Book()");
        //创建摘抄集信息
        BookInfo bookInfo = new BookInfo();
        bookInfo.bookId = IdCreator.create_BookId();
        if (bookName == null) {
            bookInfo.bookName = BookInfo.DEFAULT_NAME;
        } else {
            bookInfo.bookName = bookName;
        }
        bookInfo.dateMod = TimeUtil.getCurrentTime();
        bookInfo.tag1 = sp.appSettings.get_Tag1();
        bookInfo.tag2 = sp.appSettings.get_Tag2();
        bookInfo.tag3 = MyRandom.getColorTag();

        //将摘抄集信息加入摘抄集总表
        dao.shelfTable.add_BookInfo_To_ShelfTable(bookInfo);

        //创建摘抄集表
        dao.bookTable.create_BookTable(bookInfo.bookId);

        //创建摘抄集文件夹
        folder.bookFolder.create_BookFolder(bookInfo.bookId);

        //如果添加的是书架界面的第一本摘抄集，则自动将其设置为首页摘抄集
        if (dao.shelfTable.get_ShelfTable(SortConst.ORDER_BY_MOD, 0, 0).size()==1) {
            sp.appSettings.set_FirstPageBook(bookInfo.bookId);
//            Log.d(TAG, "第一次创建");
        }

        return bookInfo;
    }

    /**
     * 删除摘抄集
     * @param bookId 摘抄集id
     */
    public void delete_Book(String bookId) {
        Log.d(LogConst.PTAG, "delete_Book()");

        //删除摘抄集相关信息
        dao.bookTable.delete_BookTable(bookId);
        dao.shelfTable.delete_BookInfo_From_ShelfTable(bookId);

        //删除摘抄集相关文件
        folder.bookFolder.delete_BookFolder(bookId);

        //若删除的是首页摘抄集
        if (sp.appSettings.get_FirstPageBook().equals(bookId)) {
            sp.appSettings.set_FirstPageBook("");
        }
    }

    /**
     * 创建自定义分类
     */
    public void create_Tag(String tagName) {
        Log.d(LogConst.PTAG, "create_Tag()");
        sp.classifications.save_Classification(tagName);
    }

    /**
     * 删除自定义分类
     */
    public void delete_Tag(int tagId) {
        Log.d(LogConst.PTAG, "delete_Tag()");
        sp.classifications.delete_Classification(tagId);
    }
    
    public String getTagName(int tag) {
        return sp.classifications.get_Classification(tag);
    }
    public void setTag1(int tag1) {
        sp.appSettings.set_Tag1(tag1);
    }

    public int get_Tag1() {
        return sp.appSettings.get_Tag1();
    }
    
    public void set_AllTag(int tag1,int tag2) {
        sp.appSettings.set_Tag1(tag1
        );
        sp.appSettings.set_Tag2(tag2);
    }

    public int get_Tag2() {
        return sp.appSettings.get_Tag2();
    }
}
